/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nomina.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import com.nomina.entidades.Empleado;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Toshiba
 */
public class EmpleadoDAO {

    private static EmpleadoDAO dao;

    public EmpleadoDAO() {
    }

    public static EmpleadoDAO getEmpleadoDAO() {
        if (dao == null) {
            dao = new EmpleadoDAO();
        }
        return dao;
    }

    public List<Empleado> getEmpleados(Connection con) {
        List<Empleado> result = new ArrayList<Empleado>();
        try {
            PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.getEmpleados());
            ResultSet r = p.executeQuery();
            while (r.next()) {
                Empleado e = new Empleado();
                e.setCedula(r.getString(1));
                e.setNombres(r.getString(2));
                e.setApellidos(r.getString(3));
                e.setSucursal(r.getString(4));
                e.setTelefono(r.getString(5));
                e.setDireccion(r.getString(6));
                e.setCargo(r.getString(7));
                e.setActivo(r.getInt(8));
                result.add(e);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception clo) {
            }

        }
        return result;

    }

    public List<Empleado> buscarEmpleados(Connection con, String nit, String nombre) {
        List<Empleado> result = new ArrayList<Empleado>();
        try {
            PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.buscarEmpleados(nit, nombre));
            ResultSet r = p.executeQuery();
            while (r.next()) {
                Empleado e = new Empleado();
                e.setCedula(r.getString(1));
                e.setNombres(r.getString(2));
                e.setApellidos(r.getString(3));
                e.setSucursal(r.getString(4));
                e.setTelefono(r.getString(5));
                e.setDireccion(r.getString(6));
                e.setCargo(r.getString(7));
                e.setActivo(r.getInt(8));
                result.add(e);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                con.close();
            } catch (Exception clo) {
            }

        }
        return result;

    }

    public boolean ingresarEmpleado(Connection con, Empleado e) {
        boolean result = true;
        try {
            PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.ingresarEmpleado());
            p.setString(1, e.getCedula());
            p.setString(2, e.getNombres());
            p.setString(3, e.getApellidos());
            p.setString(4, e.getSucursal());
            p.setString(5, e.getTelefono());
            p.setString(6, e.getDireccion());
            p.setString(7, e.getCargo());
            p.setInt(8, e.getActivo());
            p.execute();

        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        } finally {
            try {
                con.close();
            } catch (Exception clo) {
            }

        }
        return result;

    }

    public boolean actualizarEmpleado(Connection con, Empleado e) {
        boolean result = true;
        try {
            PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.actualizarEmpleado());

            
             p.setString(1, e.getCedula());
            p.setString(2, e.getNombres());
            p.setString(3, e.getApellidos());
            p.setString(4, e.getSucursal());
            p.setString(5, e.getTelefono());
            p.setString(6, e.getDireccion());
            p.setString(7, e.getCargo());
            p.setInt(8, e.getActivo());
            p.execute();

        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        } finally {
            try {
                con.close();
            } catch (Exception clo) {
            }

        }
        return result;

    }

     public boolean cambiarEstado(Connection con,Empleado e){
            boolean result = true;
            try{
                PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.cambiarEstado());
                p.setInt(1, e.getActivo());
                p.setString(2, e.getCedula());
                p.execute();
                } catch(Exception ex){
                    ex.printStackTrace();
                    result = false;
                }finally{
                try{
                  con.close();
                }catch(Exception clo){}

                }
            return result;

	}
//     public List<Vehiculo> getVehiculos(Connection con){
//            List<Vehiculo> result = new ArrayList<Vehiculo>();
//            try{
//                PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.getVehiculos());
//                ResultSet r = p.executeQuery();
//                    while(r.next()){
//                        Vehiculo v = new Vehiculo();
//                        v.setVehiculoId(r.getInt(1));
//                        v.setMatricula(r.getString(2));
//                        v.setModelo(r.getString(3));
//                        v.setEmpleado(r.getString(4));
//
//                        result.add(v);
//                    }
//                } catch(Exception ex){
//                    ex.printStackTrace();
//                }finally{
//                try{
//                  con.close();
//                }catch(Exception clo){}
//
//                }
//            return result;
//
//	}
//     public List<Vehiculo> getVehiculosEmpleado(Connection con,String nit){
//            List<Vehiculo> result = new ArrayList<Vehiculo>();
//            try{
//                PreparedStatement p = con.prepareStatement(EmpleadoDAOHelper.getVehiculosEmpleado());
//                p.setString(1, nit);
//                ResultSet r = p.executeQuery();
//                    while(r.next()){
//                        Vehiculo v = new Vehiculo();
//                        v.setVehiculoId(r.getInt(1));
//                        v.setMatricula(r.getString(2));
//                        v.setModelo(r.getString(3));
//                        v.setEmpleado(r.getString(4));
//
//                        result.add(v);
//                    }
//                } catch(Exception ex){
//                    ex.printStackTrace();
//                }finally{
//                try{
//                  con.close();
//                }catch(Exception clo){}
//
//                }
//            return result;
//
//	}
    /**
     * @return the dao
     */
    public EmpleadoDAO getDao() {
        return dao;
    }

    /**
     * @param dao the dao to set
     */
    public void setDao(EmpleadoDAO dao) {
        this.dao = dao;
    }
}
